#!/usr/bin/env python
# coding: utf-8

# # 请添加自己的实验代码，点击运行查看结果

# In[99]:


import requests
from bs4 import BeautifulSoup
import pandas as pd
import re


# In[100]:


file_path='work_url.txt'
with open(file_path,'r',encoding='utf-8')as file:
    url = file.readline().strip()
# 发送GET请求
response = requests.get(url)
print(url)


# In[101]:


# 检查请求是否成功
if response.status_code == 200:
    # 设置编码
    response.encoding = 'utf-8'
    # 解析HTML内容
    soup = BeautifulSoup(response.text, 'lxml')
    # 查找所有的书籍标题，这里假设每个标题都在<dd>标签中的<a>标签内
    titles = [title.text for title in soup.find_all('dd') if title.a]
    
    # 使用列表推导式来筛选符合正则表达式的章节标题
    chapter_titles = [title for title in titles if re.match(r"第\d+章", title)]

    # 初始化分割后的章节号和标题列表
    chapter_numbers = []
    chapter_titles_split = []


# In[102]:


# 分割章节标题，并收集分割后的章节号和标题
    for title in chapter_titles:
        split_result = title.split(' ', 1)
        if len(split_result) == 2:
            chapter_number, chapter_title = split_result
        else:
            # 处理没有空格的情况
            chapter_number = title
            chapter_title = ""
        chapter_numbers.append(chapter_number)
        chapter_titles_split.append(chapter_title)


# In[103]:


# 创建DataFrame并输出到Excel文件
    df = pd.DataFrame({
        '章节号': chapter_numbers,
        '标题': chapter_titles_split
    })
    # 输出到Excel文件
    output_file = 'output.xlsx'
    df


# In[104]:


####第二数据


# In[105]:


with open(file_path,'r',encoding='utf-8')as file:
    file.readline().strip()
    url2=file.readline().strip()
text = requests.get(url2).content.decode('GBK',errors='ignore')
print(text)


# In[106]:


main_page = BeautifulSoup(text, 'html.parser')
print(url)


# In[107]:


table = main_page.find('table')

# 初始化列表，用于存储电影数据
movies_data = []


# In[108]:


trs = table.find_all('tr')


# In[109]:


for tr in trs:
    lst = tr.find_all('td')
    if len(lst) != 0:  # 过滤空行
        # 提取并处理数据，去除不需要的文本和逗号
        movie_data = [td.text.strip().replace('全球电影票房排行榜', '').replace(',', '') for td in lst]
        movies_data.append(movie_data)


# In[110]:


# 创建DataFrame
df2 = pd.DataFrame(movies_data)


# In[111]:


# 写入Excel文件，指定sheet_name和index参数
with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
    # 将df写入到'Sheet1'工作表
    df.to_excel(writer, sheet_name='Sheet1', index=False)
    # 将df2写入到'Sheet2'工作表
    df2.to_excel(writer, sheet_name='Sheet2', index=False)


# In[ ]:





# In[ ]:





# In[ ]:




